﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using FanaticRock.Core.Interfaces;
using FanaticRock.Core.Models;
using FanaticRock.Core.ServiceInterfaces;
using FanaticRock.Extensions;
using FanaticRock.Helpers;
using FanaticRock.Interfaces;
using System.Collections.ObjectModel;
using System.Windows.Media;

namespace FanaticRock.ViewModels
{
    [INotifyPropertyChanged]
    public partial class SongViewModel : IFromModel<Song>, IHasIndex
    {
        #region dependency

        protected IPlayListService _playListService;

        #endregion

        #region props

        [ObservableProperty]
        protected string _id;

        [ObservableProperty]
        protected string _title;

        [ObservableProperty]
        protected string _originTitle;

        [ObservableProperty]
        protected string _albumId;

        [ObservableProperty]
        protected string _albumTitle;

        [ObservableProperty]
        protected TimeSpan _duration;

        [ObservableProperty]
        protected Uri _uri;

        public ResourceSource CoverSource { get => _coverSource; set => SetCoverSource(value); }
        protected ResourceSource _coverSource;

        [ObservableProperty]
        protected ObservableCollection<SongMemberViewModel> _members;

        public int IndexInCollection { get => _indexInCollection; set => SetProperty(ref _indexInCollection, value); }
        protected int _indexInCollection;

        #endregion

        #region props

        [ObservableProperty]
        private Brush _coverSourceBrush;

        #endregion

        #region ctors

        public SongViewModel()
        {
            _playListService = Ioc.Default.GetService<IPlayListService>();
        }

        public SongViewModel(SongViewModel viewModel) : this()
        {
            _id = viewModel.Id;
            _title = viewModel.Title;
            _originTitle = viewModel.OriginTitle;
            _albumId = viewModel.AlbumId;
            _albumTitle = viewModel.AlbumTitle;
            _duration = viewModel.Duration;
            _uri = viewModel.Uri;
            CoverSource = viewModel.CoverSource;
            _members = viewModel.Members;

            IndexInCollection = viewModel.IndexInCollection;
        }

        public SongViewModel(Song song) : this()
        {
            Load(song);
        }

        #endregion

        #region from model

        private void SetCoverSource(ResourceSource value)
        {
            _coverSource = value;
            OnPropertyChanged(nameof(CoverSource));

            //CoverSourceBrush = value.CreateImageBrush();
        }

        public void Load(Song model)
        {
            _id = model.Id;
            _title = model.Title;
            _originTitle = model.OriginTitle;
            _albumId = model.AlbumId;
            _albumTitle = model.AlbumTitle;
            _duration = model.Duration;
            _uri = model.Uri;
            CoverSource = model.GetCoverSource();
            _members = ObservableCollectionHelper.CreateFromModel<SongMember, SongMemberViewModel>(model.Members);

        }



        public Song Export()
        {
            return new Song()
            {
                Id = _id,
                Title = _title,
                OriginTitle = _originTitle,
                AlbumId = _albumId,
                AlbumTitle = _albumTitle,
                Duration = _duration,
                Uri = _uri,
                CoverSource = _coverSource.Clone() as ResourceSource,
                Members = ObservableCollectionHelper.ExportFromModel<SongMember, SongMemberViewModel>(_members),
            };
        }


        #endregion


        #region methods

        [RelayCommand]
        public virtual async void Play()
        {
            //_audioPlayService.Open(Uri);
            //_audioPlayService.Play();

            var song = await _playListService.GetSongById(_id);
           
            if (song == null)
            {
                song = Export();
              await   _playListService.AddSong(song);
            }

            _playListService.Play(song);

        }
        #endregion

    }
}
